package com.czl.module_service.fragment.assetHandle

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.AssetHandleSortStoreAdapter
import com.czl.module_service.databinding.FragmentAssetHandleSortStoreBinding
import com.czl.module_service.extension.loadCompletedData
import com.czl.module_service.view.ListSheetDialogFragment
import com.czl.module_service.viewmodel.AssetHandleSortStoreViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_SORT_STORE)
class AssetHandleSortStoreFragment : BaseFragment<FragmentAssetHandleSortStoreBinding, AssetHandleSortStoreViewModel>() {

    private lateinit var sAdapter: AssetHandleSortStoreAdapter

    override fun initContentView(): Int {
        return R.layout.fragment_asset_handle_sort_store
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        initAdapter()
        reload()
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private fun initAdapter() {
        sAdapter = AssetHandleSortStoreAdapter().apply {
        }
        binding.ryCommon.apply{
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = sAdapter
        }
        sAdapter.productIdList =
            arguments?.getIntegerArrayList("productIdList") as ArrayList<Int>
        sAdapter.sortStoreIddList =
            arguments?.getStringArrayList("sortStoreIdList") as ArrayList<String>
        sAdapter.type = arguments?.getSerializable("type") as String

        viewModel.parentCompanyId = arguments?.getSerializable("parentCompanyId") as Int
        viewModel.type = arguments?.getSerializable("type") as String
        LiveBusCenter.observeAddAssetHanldeSortSelectEvent(this, {
            viewModel.sortIdInfoList = it.data as HashMap<String, Int>
        })
    }

    override fun initViewObservable() {
        viewModel.uc.loadSortEvent.observe(this, { data ->
            this.loadCompletedData(
                false,
                viewModel.currentPage == 2,
                data != null,
                1,
                data,
                sAdapter,
                binding.ryCommon,
                binding.smartCommon
            )
        })
        viewModel.uc.loadProductEvent.observe(this, { data ->
            this.loadCompletedData(
                data?.hasNextPage,
                viewModel.currentPage == 2,
                data != null,
                data?.total,
                data?.list,
                sAdapter,
                binding.ryCommon,
                binding.smartCommon
            )
        })
        viewModel.uc.loadSubmitEvent.observe(this, {data ->
            LiveBusCenter.postAddAssetHanldeSortEvent(data,viewModel.type,viewModel.manageMode)
        })
        viewModel.uc.loadChangeEvent.observe(this, {data ->
            if (data == 0) {
                viewModel.currentPage = 1
                getDataInfo()
            } else if (data == 1) {
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    viewModel.storehouseNameList
                ) {
                    viewModel.storehouseInfo.set(viewModel.storehouseNameList[it])
                    var comBean = viewModel.sStorehouseList[it]
                    viewModel.storehouseId = comBean.storehouseId
                    viewModel.currentPage = 1
                    getDataInfo()
                }
            } else  if (data == 2) {
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    viewModel.sortNameList
                ) {
                    viewModel.sortInfo.set(viewModel.sortNameList[it])
                    var comBean = viewModel.sSortList[it]
                    viewModel.sortId = comBean.sortId
                    viewModel.manageMode = comBean.manageMode
                    viewModel.currentPage = 1
                    getDataInfo()
                }
            }
        })
    }

    fun getDataInfo () {
        sAdapter.setList(arrayListOf())
        viewModel.getDataList()
    }

    override fun reload() {
        viewModel.getStorehouseList()
        viewModel.getSortList()
//        binding.smartCommon.autoRefresh()
    }
}